package com.yanqu.road.server.manger.union.listener;

import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.manger.ICrossUnionSyncListener;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityRankMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;

import java.util.ArrayList;
import java.util.List;

/**
 * 盐场活动监听
 */
public class OreWarListener implements ICrossUnionSyncListener {

    @Override
    public void notifySync(int syncType, long playerId, CrossUnionInfo unionInfo, List<CrossUnionMember> memberList) {


        List<Long> userIdList = new ArrayList<>();
        for (CrossUnionMember member : memberList) {
            if(member.getServerId() == GameServer.getInstance().getServerId()){
                userIdList.add(member.getUserId());
            }
        }

        if(syncType == eUnionSyncType.BeKick.getValue() || syncType == eUnionSyncType.QuitUnion.getValue() || syncType == eUnionSyncType.JoinUnion.getValue()
                || syncType == eUnionSyncType.CreateUnion.getValue()){

            for (CrossUnionMember member : memberList) {
                if(member.getServerId() == GameServer.getInstance().getServerId()) {
                    //结算没冠名奖励
                    OreWarMgr.sendUnionNamingJoinQuitReward(unionInfo.getUnionUid(), member.getUserId());
                }
            }


            //跨服刷新带上用户基础信息，用来判断变化的冠名
            if(userIdList.size() > 0) {
                OreWarMgr.refreshCrossOreStation(userIdList);
            }

        }

        if(syncType == eUnionSyncType.Normal.getValue()){
            if(userIdList.size() == 0){
                CrossUnionMember member = CrossUnionMgr.getUnionMember(unionInfo.getUnionUid(), playerId);
                if(member != null && member.getServerId() == GameServer.getInstance().getServerId()){
                    userIdList.add(member.getUserId());
                    //跨服刷新带上用户基础信息，用来判断变化的冠名
                    //原来的PlayerModule.onUserInfoChange
//                    OreWarMgr.refreshCrossOreStation(userIdList);
                }
            }else {
                //跨服刷新带上用户基础信息，用来判断变化的冠名
//                OreWarMgr.refreshCrossOreStation(userIdList);
            }
        }

        if(syncType == eUnionSyncType.DeleteUnion.getValue()){

            if(unionInfo.getServerId() == GameServer.getInstance().getServerId()) {
                //解散发放冠名奖励
                OreWarMgr.sendUnionDismissNamingChangeReward(unionInfo.getUnionUid());
            }

            //跨服刷新带上用户基础信息，用来判断变化的冠名
            if(userIdList.size() > 0) {
                OreWarMgr.refreshCrossOreStation(userIdList);
            }
        }


        if(syncType == eUnionSyncType.CreateUnion.getValue() || syncType == eUnionSyncType.DeleteUnion.getValue() ||
                syncType == eUnionSyncType.QuitUnion.getValue() || syncType == eUnionSyncType.BeKick.getValue() ||
                syncType == eUnionSyncType.JoinUnion.getValue()) {

            //有人退出刷新下商会坑位状态，因为冠名可能会改变
            OreWarMgr.refreshOreStation();
        }

        if(syncType == eUnionSyncType.CreateUnion.getValue() || syncType == eUnionSyncType.DeleteUnion.getValue() ||
                syncType == eUnionSyncType.Normal.getValue() ||
                syncType == eUnionSyncType.JoinUnion.getValue()) {

            for (long userId : userIdList) {
                //实时更新榜单上的用户的家族信息
                OreWarActivityRankMgr.changeUserInfo(userId);
            }
        }
    }
}
